package com.svo.weitu.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.svo.platform.utils.HttpUtil;
import com.svo.platform.utils.SvoUtil;
import com.svo.platform.widget.LoadDialog;
import com.svo.weitu.App;
import com.svo.weitu.R;
import com.svo.weitu.util.Cons;
import com.svo.weitu.util.MyUtil;
import com.svo.weitu.util.UiUtil;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class FeedbackActivity extends AppCompatActivity {
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_back_normal);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText = (EditText) findViewById(R.id.editText2);
                String input = editText.getText().toString();
                if (!TextUtils.isEmpty(input)) {
                    if (input.length() > 200) {
                        UiUtil.toast(getApplicationContext(), "反馈内容不可超过200个字");
                    } else {
                        submit(input);
                    }
                } else {
                    UiUtil.toast(getApplicationContext(), "反馈内容不可为空");
                }
            }
        });
    }

    private void submit(String input) {
        if (TextUtils.isEmpty(input)) {
            UiUtil.toast(App.context,"内容不可为空");
            return;
        }
        if (input.matches("\\d+") || input.length() < 4) {
            UiUtil.toast(App.context,"请仔细填写反馈内容");
            return;
        }
        RequestParams params = new RequestParams();
        int version = SvoUtil.getVersion(getApplicationContext());
        params.put("flag","feedback");
        try {
            params.put("content", URLEncoder.encode(input,"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        params.put("app","weitu");
        params.put("version",""+version);
        params.put("devId",""+ MyUtil.getDevId(getApplicationContext()));
        HttpUtil.post(Cons.gen+"FeedBack",params,new JsonHttpResponseHandler(){
            LoadDialog loadDialog;
            @Override
            public void onStart() {
                super.onStart();
                if (loadDialog == null)
                    loadDialog = new LoadDialog(FeedbackActivity.this);
                loadDialog.show();
            }

            @Override
            public void onSuccess(JSONObject jsonObject) {
                super.onSuccess(jsonObject);
                if (jsonObject != null && jsonObject.optInt("type", 1) == 0) {
                    UiUtil.toast(getApplicationContext(),jsonObject.optString("msg"));
                }else {
                    UiUtil.toast(getApplicationContext(),"请求失败");
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (loadDialog != null)
                    loadDialog.dismiss();
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
